using System;
using System.Drawing;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;
using OpenNETCF.Windows.Forms;

namespace CHMReader
{
	/// <summary>
	/// Summary description for HTMLView.
	/// </summary>
	/// 
	
	public enum EnumWhereNext { Prev, Next, Close, Link, None};


	public class HTMLView : System.Windows.Forms.Form
	{
		public	EnumWhereNext WhereNext=EnumWhereNext.Close;
		public string Link="";
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem mnuTools;
		private System.Windows.Forms.MenuItem mnuTextSize;
		private System.Windows.Forms.MenuItem mnuSmallest;
		private System.Windows.Forms.MenuItem mnuSmall;
		private System.Windows.Forms.MenuItem mnuMedium;
		private System.Windows.Forms.MenuItem mnuLarge;
		private System.Windows.Forms.MenuItem mnuLargest;
		private System.Windows.Forms.MenuItem mnuClearType;
		private System.Windows.Forms.MenuItem mnuFitToWindow;
		private System.Windows.Forms.MenuItem mnuAbout;
		protected OpenNETCF.Windows.Forms.HTMLViewer HTML;

		private string m_Anchor="";
		private string regClearType="ClearType";
		private string regTextSize="TextSize";
        private System.Windows.Forms.MenuItem mnuHelp;
		private System.Windows.Forms.TextBox lbCHMTitle;
        private MenuItem mnuPrev;
        private MenuItem mnuNext;
		private string regFitToWindow="FitToWindow";
	
		public HTMLView()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
						
			HTML=new OpenNETCF.Windows.Forms.HTMLViewer();
			HTML.Top=lbCHMTitle.Top+lbCHMTitle.Height;
			HTML.Left=0;
			HTML.Width=this.Width;
			HTML.Height=this.Height-lbCHMTitle.Height;
			Controls.Add(HTML);
			HTML.Source="";	
			HTML.HotSpotClick+=new OpenNETCF.Windows.Forms.HTMLViewer.HotSpotClickEventHandler(HTML_HotSpotClick);
					
			OpenNETCF.Win32.RegistryKey RegKey=null;
			try
			{
				RegKey=OpenNETCF.Win32.Registry.CurrentUser.CreateSubKey("CHMReader");

				SetTextSize(Convert.ToInt32(RegKey.GetValue(regTextSize,2).ToString()));

				bool bBool=false;
				int iInt=Convert.ToInt32(RegKey.GetValue(regClearType,0));
				if (iInt!=0)
					bBool=true;
				SetClearType(bBool);

				iInt=Convert.ToInt32(RegKey.GetValue(regFitToWindow,0));
				if (iInt!=0)
					bBool=true;				
				SetFitToWindow(bBool);
				RegKey.Close();
			}
			catch(Exception ex)
			{
				RegKey.Close();
				Debug.WriteLine(ex.Message);
			}			
		}

		public void SetTitle(string Title)
		{
			lbCHMTitle.Text=Title;
		}

		public void SetHTMLText(string HTMLText)
		{
			m_Anchor="";
			HTML.Source=HTMLText;
		}

		public void SetUrl(string Url)
		{
			m_Anchor="";
			HTML.Url=Url;
		}

		public void NavButtons(bool bEnableNavButtons)
		{
			mnuNext.Enabled=bEnableNavButtons;
			mnuPrev.Enabled=bEnableNavButtons;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.mnuTools = new System.Windows.Forms.MenuItem();
            this.mnuClearType = new System.Windows.Forms.MenuItem();
            this.mnuTextSize = new System.Windows.Forms.MenuItem();
            this.mnuSmallest = new System.Windows.Forms.MenuItem();
            this.mnuSmall = new System.Windows.Forms.MenuItem();
            this.mnuMedium = new System.Windows.Forms.MenuItem();
            this.mnuLarge = new System.Windows.Forms.MenuItem();
            this.mnuLargest = new System.Windows.Forms.MenuItem();
            this.mnuFitToWindow = new System.Windows.Forms.MenuItem();
            this.mnuHelp = new System.Windows.Forms.MenuItem();
            this.mnuAbout = new System.Windows.Forms.MenuItem();
            this.lbCHMTitle = new System.Windows.Forms.TextBox();
            this.mnuPrev = new System.Windows.Forms.MenuItem();
            this.mnuNext = new System.Windows.Forms.MenuItem();
            this.SuspendLayout();
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.Add(this.mnuTools);
            this.mainMenu1.MenuItems.Add(this.mnuHelp);
            // 
            // mnuTools
            // 
            this.mnuTools.MenuItems.Add(this.mnuClearType);
            this.mnuTools.MenuItems.Add(this.mnuTextSize);
            this.mnuTools.MenuItems.Add(this.mnuFitToWindow);
            this.mnuTools.MenuItems.Add(this.mnuPrev);
            this.mnuTools.MenuItems.Add(this.mnuNext);
            this.mnuTools.Text = "Tools";
            // 
            // mnuClearType
            // 
            this.mnuClearType.Text = "ClearType";
            this.mnuClearType.Click += new System.EventHandler(this.mnuClearType_Click);
            // 
            // mnuTextSize
            // 
            this.mnuTextSize.MenuItems.Add(this.mnuSmallest);
            this.mnuTextSize.MenuItems.Add(this.mnuSmall);
            this.mnuTextSize.MenuItems.Add(this.mnuMedium);
            this.mnuTextSize.MenuItems.Add(this.mnuLarge);
            this.mnuTextSize.MenuItems.Add(this.mnuLargest);
            this.mnuTextSize.Text = "Text Size";
            // 
            // mnuSmallest
            // 
            this.mnuSmallest.Text = "Smallest";
            this.mnuSmallest.Click += new System.EventHandler(this.mnuSmallest_Click);
            // 
            // mnuSmall
            // 
            this.mnuSmall.Text = "Small";
            this.mnuSmall.Click += new System.EventHandler(this.mnuSmall_Click);
            // 
            // mnuMedium
            // 
            this.mnuMedium.Text = "Medium";
            this.mnuMedium.Click += new System.EventHandler(this.mnuMedium_Click);
            // 
            // mnuLarge
            // 
            this.mnuLarge.Text = "Large";
            this.mnuLarge.Click += new System.EventHandler(this.mnuLarge_Click);
            // 
            // mnuLargest
            // 
            this.mnuLargest.Text = "Largest";
            this.mnuLargest.Click += new System.EventHandler(this.mnuLargest_Click);
            // 
            // mnuFitToWindow
            // 
            this.mnuFitToWindow.Text = "Fit To Window";
            this.mnuFitToWindow.Click += new System.EventHandler(this.mnuFitToWindow_Click);
            // 
            // mnuHelp
            // 
            this.mnuHelp.MenuItems.Add(this.mnuAbout);
            this.mnuHelp.Text = "Help";
            // 
            // mnuAbout
            // 
            this.mnuAbout.Text = "About CHMReader.Net";
            this.mnuAbout.Click += new System.EventHandler(this.mnuAbout_Click);
            // 
            // lbCHMTitle
            // 
            this.lbCHMTitle.Location = new System.Drawing.Point(0, 0);
            this.lbCHMTitle.Name = "lbCHMTitle";
            this.lbCHMTitle.ReadOnly = true;
            this.lbCHMTitle.Size = new System.Drawing.Size(240, 30);
            this.lbCHMTitle.TabIndex = 0;
            this.lbCHMTitle.Text = "textBox1";
            this.lbCHMTitle.WordWrap = false;
            // 
            // mnuPrev
            // 
            this.mnuPrev.Text = "Previous";
            this.mnuPrev.Click += new System.EventHandler(this.mnuPrev_Click);
            // 
            // mnuNext
            // 
            this.mnuNext.Text = "Next";
            this.mnuNext.Click += new System.EventHandler(this.mnuNext_Click);
            // 
            // HTMLView
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.ClientSize = new System.Drawing.Size(240, 266);
            this.Controls.Add(this.lbCHMTitle);
            this.Menu = this.mainMenu1;
            this.Name = "HTMLView";
            this.Closed += new System.EventHandler(this.HTMLView_Closed);
            this.ResumeLayout(false);

		}
		#endregion

		private void mnuAbout_Click(object sender, System.EventArgs e)
		{
			AboutDialog Dlg=new AboutDialog();
			Dlg.ShowDialog();			
		}

		private void SetClearType(bool bEnableClearType)
		{
			OpenNETCF.Win32.RegistryKey RegKey=OpenNETCF.Win32.Registry.CurrentUser.CreateSubKey("CHMReader");
			RegKey.SetValue(regClearType,Convert.ToInt32(bEnableClearType));
			RegKey.Close();

			mnuClearType.Checked=bEnableClearType;
			HTML.EnableClearType=bEnableClearType;
		}

		private void mnuClearType_Click(object sender, System.EventArgs e)
		{
			SetClearType(!mnuClearType.Checked);
		}

		private void SetFitToWindow(bool bEnableFitToWindow)
		{
			OpenNETCF.Win32.RegistryKey RegKey=OpenNETCF.Win32.Registry.CurrentUser.CreateSubKey("CHMReader");
			RegKey.SetValue(regFitToWindow,Convert.ToInt32(bEnableFitToWindow));			
			RegKey.Close();
			HTML.ShrinkMode=bEnableFitToWindow;
			mnuFitToWindow.Checked=bEnableFitToWindow;
		}

		private void mnuFitToWindow_Click(object sender, System.EventArgs e)
		{			
			SetFitToWindow(!mnuFitToWindow.Checked);
		}

		private void SetTextSize(int ZoomLevel)
		{
			OpenNETCF.Win32.RegistryKey RegKey=OpenNETCF.Win32.Registry.CurrentUser.CreateSubKey("CHMReader");					
			RegKey.SetValue(regTextSize,ZoomLevel);
			RegKey.Close();
			HTML.ZoomLevel=ZoomLevel;
			mnuSmallest.Checked=(ZoomLevel==0);
			mnuSmall.Checked=(ZoomLevel==1);
			mnuMedium.Checked=(ZoomLevel==2);
			mnuLarge.Checked=(ZoomLevel==3);
			mnuLargest.Checked=(ZoomLevel==4);
		}

		private void mnuSmallest_Click(object sender, System.EventArgs e)
		{
			SetTextSize(0);
		}

		private void mnuSmall_Click(object sender, System.EventArgs e)
		{
			SetTextSize(1);
		}

		private void mnuMedium_Click(object sender, System.EventArgs e)
		{
			SetTextSize(2);
		}

		private void mnuLarge_Click(object sender, System.EventArgs e)
		{
			SetTextSize(3);
		}

		private void mnuLargest_Click(object sender, System.EventArgs e)
		{
			SetTextSize(4);
		}

		private void mnuPrev_Click(object sender, System.EventArgs e)
		{
			WhereNext=EnumWhereNext.Prev;
			base.Close();
		}

		private void mnuNext_Click(object sender, System.EventArgs e)
		{			
			WhereNext=EnumWhereNext.Next;
			base.Close();
		}

		private void HTML_HotSpotClick(object sender, HotSpotClickEventArgs e)
		{		
			Link=e.Url;
			WhereNext=EnumWhereNext.Link;
			base.Close();
		}

		private void HTMLView_Closed(object sender, System.EventArgs e)
		{				
			base.Close();
		}

		public void SetAnchor(string Anchor)
		{
			m_Anchor=Anchor;			
		}

		public DialogResult ShowDialogEx()
		{
			WhereNext=EnumWhereNext.Close;
			HTML.JumpAnchor(m_Anchor);
			HTML.Focus();
			return base.ShowDialog();
		}
	}
}
